package com.paireach.szwolaiyun.fragmentall;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.paireach.szwolaiyun.R;
import com.paireach.szwolaiyun.sz_common_widget.fragment.BaseFragment;
import com.paireach.szwolaiyun.util.BaiDuSimpleLocationHelp;

import java.lang.ref.WeakReference;

import szwolaiyun.paireach.com.szwolaizhuang.model.business.DriveOrder;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.AppEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpErrorEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpSuccessEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.requestTag.RequestTag;
import szwolaiyun.paireach.com.szwolaizhuang.net.WayBillRequest;
import szwolaiyun.paireach.com.szwolaizhuang.net.json.JsonPaser;

/**
 * 直发客户收货
 * Created by Administrator on 2016/7/4.
 */
public class StraightHairSignFragment extends BaseFragment implements View.OnClickListener {
    public static final String TAG = "StraightHairSignFragment";
    private Button sign;

    private View view;
    private DriveOrder driveOrder;
    private BaiDuSimpleLocationHelp baiDuSimpleLocationHelp;
    private MyHandler handler;

    public static StraightHairSignFragment getInstance(DriveOrder driveOrder) {
        StraightHairSignFragment fragment = new StraightHairSignFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("DriveOrder", driveOrder);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            driveOrder = (DriveOrder) bundle.getSerializable("DriveOrder");
        }
        baiDuSimpleLocationHelp = new BaiDuSimpleLocationHelp();
        handler = new MyHandler(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_straight_hair_sign, null);
        sign = (Button) view.findViewById(R.id.sign);
        sign.setOnClickListener(this);
        view.findViewById(R.id.layout_top_back).setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        ((TextView) view.findViewById(R.id.layout_top_change)).setText("送货在途");
        return view;
    }

    @Override
    public void onClick(View view) {
        if (view == sign) {
            showProgressDialog();
            baiDuSimpleLocationHelp.startGetLocation(getActivity(), handler, null);
        }
    }

    @Override
    public void applicationEvent(AppEvent event) {
        super.applicationEvent(event);
        if (event.getTag().equals("judge_sign_state")) {
            showToast(event.getExtraInfo());
            if (event.getCode() == 0) {
                getActivity().finish();
            }
        }
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        if (event.getRequestTag() == RequestTag.SIGN) {
            dismissProgressDialog();
            showToast(event.getErrorMessage());
        }
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        if (event.getRequestTag() == RequestTag.SIGN) {
            //签到
            dismissProgressDialog();
            JsonPaser.parseSignInfo(event.getJson());
        }
    }

    private static class MyHandler extends Handler {

        private WeakReference<StraightHairSignFragment> mm;

        public MyHandler(StraightHairSignFragment service) {
            mm = new WeakReference<>(service);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            StraightHairSignFragment service = mm.get();
            if (service != null) {
                switch (msg.what) {
                    case BaiDuSimpleLocationHelp.getLocationSucceed:
                        BDLocation location = (BDLocation) msg.obj;
                        new WayBillRequest().sign(service.driveOrder.getOrderNo(), location.getLatitude(), location.getLongitude());
                        break;
                }
            }
        }
    }
}
